package com.zhengsanming.service;

import com.zhengsanming.mapper.BrandMapper;
import com.zhengsanming.util.SqlsessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.zhengsanming.pojo.Brand;

import java.util.List;

public class BrandService {
    SqlSessionFactory sqlSessionFactory = SqlsessionFactoryUtils.getSqlSessionFactory();

    /*
     * 查询所有
     *
     *  */
    public List<Brand> selectAll() {
        //获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取BrandMapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brands = mapper.selectAll();
        sqlSession.close();
        return brands;
    }

    /*
     * 添加
     * 1、*/
    public void add(Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取BrandMapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.add(brand);
        sqlSession.commit();
        sqlSession.close();
    }

    /*
     * 查询一个
     * */
    public Brand selectById(int id) {
        //获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取BrandMapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        Brand brands = mapper.selectByIdBrand(id);
        sqlSession.close();
        return brands;
    }

    /*
     * 修改
     * */
    public void update(Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取BrandMapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.update(brand);
        sqlSession.commit();
        sqlSession.close();
    }

}
